//
//  LJEssenceViewController.m
//  百思不得姐
//
//  Created by 刘鹿杰 on 16/7/3.
//  Copyright © 2016年 刘鹿杰. All rights reserved.
//

#import "LJEssenceViewController.h"
#import "UIView+LJExtension.h"
#import "UIBarButtonItem+LJExtension.h"
#import "LJIViewcotroller.h"
#import "LJBasicTableViewController.h"





@interface LJEssenceViewController () <UIScrollViewDelegate>

/** 顶部的View */
@property (nonatomic, strong) UIView *titleView;
// 被选中的btn记录
@property (nonatomic, strong)UIButton *SelectedButton;

// 底部的选中下划线
@property (nonatomic, strong)UIView *indicatorView;

/**内容页面 contentScollView  */
@property (nonatomic, strong) UIScrollView *contentScollView;

@end

@implementation LJEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置navigation
    [self setUpNavigatinon];
    
    // 添加自控制器
    [self setUpchildVC];
    
    // 设置顶部的titleView
    [self setUpTitleView];
    
    //设置中部的ScrollView
    [self setUpScrollView];
    
    
}

// 设置navigation
-(void)setUpNavigatinon{
    
    // 设置标题图片
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    // 设置左侧标识符号
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"MainTagSubIcon" hightedImage:@"MainTagSubIconClick" target:self action:@selector(essenceBtnClick:)];
    self.view.backgroundColor = LJGlobalBg;

}

// 添加子控制器
-(void)setUpchildVC{
    
    LJBasicTableViewController *allVC = [[LJBasicTableViewController alloc] init];
    allVC.type = LJTopicTypeAll;
    allVC.title = @"全部";
    [self addChildViewController:allVC];
    
    LJBasicTableViewController *videoVC = [[LJBasicTableViewController alloc] init];
    videoVC.type = LJTopicTypeVideo;
    videoVC.title = @"视频";
    [self addChildViewController:videoVC];
    
    LJBasicTableViewController *voiceVC = [[LJBasicTableViewController alloc] init];
    voiceVC.type = LJTopicTypeVoice;
    voiceVC.title = @"声音";
    [self addChildViewController:voiceVC];
    
    LJBasicTableViewController *pictureVC = [[LJBasicTableViewController alloc] init];
    pictureVC.type = LJTopicTypePicture;
    pictureVC.title = @"图片";
    [self addChildViewController:pictureVC];
    
    LJBasicTableViewController *wordVC = [[LJBasicTableViewController alloc] init];
    wordVC.type = LJTopicTypeWord;
    wordVC.title = @"段子";
    [self addChildViewController:wordVC];
    
}

// 设置顶部的titleView
- (void)setUpTitleView{
    
    //  创建titleView
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
    titleView.width = KScreenW;
    titleView.height = 35.0;
    titleView.y = 64;
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    
    // 创建底部的红色指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.height = 2;
    indicatorView.y = titleView.height - indicatorView.height;
    indicatorView.backgroundColor = [UIColor redColor];
    self.indicatorView = indicatorView;
    
    for (int i = 0 ; i < self.childViewControllers.count; i++) {
        
       UIButton *SubButton = [[UIButton alloc] init];
        
        SubButton.width = KScreenW / self.childViewControllers.count;
        SubButton.x = SubButton.width * i;
        SubButton.height = titleView.height;
        SubButton.tag = i;
        UIViewController *VC = self.childViewControllers[i];
        [SubButton setTitle:VC.title forState:UIControlStateNormal];
        
        [SubButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [SubButton setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
         SubButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [SubButton addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:SubButton];
    
        // 默认选中第 0 个按钮
        if (i == 0) {
            
            SubButton.enabled = NO;
            
            self.SelectedButton = SubButton;
            
            [SubButton.titleLabel sizeToFit];
            
            indicatorView.width = SubButton.titleLabel.width;
            
            indicatorView.centerx = SubButton.centerx;

        }
        
    }
    [titleView addSubview:indicatorView];
}

// 设置内容的ScrollView
-(void)setUpScrollView{

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *contentScollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    contentScollView.delegate = self;
    contentScollView.contentSize = CGSizeMake(KScreenW * self.childViewControllers.count, 0);
    contentScollView.pagingEnabled = YES;
    self.contentScollView = contentScollView;
    
    [self.view insertSubview:contentScollView atIndex:0];
    
    // 选中第一页 内容
    [self scrollViewDidEndScrollingAnimation:contentScollView];
}



// btn 的单击事件
-(void)titleBtnClick:(UIButton *)button{
    
    self.SelectedButton.enabled = YES;
    
    button.enabled = NO;
   
    self.SelectedButton = button;
    
    [UIView animateWithDuration:0.25 animations:^{
         self.indicatorView.width = button.titleLabel.width;
         self.indicatorView.centerx = button.centerx;
    }];
    
    CGPoint offset = self.contentScollView.contentOffset;
    
    offset.x = button.tag * self.contentScollView.width;
    
    [self.contentScollView setContentOffset:offset animated:YES];
}



//  跳转到下一个控制器
-(void)essenceBtnClick:(UIButton *)btn {
   
    LJIViewcotroller * VC = [[LJIViewcotroller alloc] init];
    
    [self.navigationController pushViewController:VC animated:YES];

}


#pragma mark -- scrollView Delegete

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    NSUInteger index = scrollView.contentOffset.x / self.view.width;
    
    UITableViewController *tableVC = self.childViewControllers[index];
    tableVC.view.x = scrollView.contentOffset.x;
    tableVC.view.y = 0 ;
    tableVC.view.height = scrollView.height;
    tableVC.view.width = scrollView.width;
    
    // 设置UItableView的内边距
    CGFloat top = CGRectGetMaxY(self.titleView.frame);
    CGFloat bottom = self.tabBarController.tabBar.height;
    tableVC.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    tableVC.tableView.scrollIndicatorInsets = tableVC.tableView.contentInset;
    [scrollView addSubview:tableVC.view];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    // 将按钮点亮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self titleBtnClick:self.titleView.subviews[index]];
}



@end
